package com.afkt.project.ui.slice;


import com.afkt.project.assist.ViewAssist;
import com.afkt.project.base.app.BaseActivity;
import com.afkt.project.common.DevFinal;
import com.afkt.project.model.item.ButtonValue;
import com.afkt.project.ui.widget.render.circle.jump.CircleBroodLoadingRenderer;
import com.afkt.project.ui.widget.render.scenery.LoadingViewDiv;
import com.afkt.project.ui.widget.render.shapechange.CoolWaitLoadingRenderer;
import com.afkt.project.ui.widget.render.shapechange.DanceLoadingRenderer;
import com.afkt.project.util.LogUtils;
import com.ohos.afkt.ResourceTable;
import com.dev.utils.app.HandlerUtils;
import com.dev.utils.app.SizeUtils;
import com.dev.utils.app.ViewUtils;
import com.dev.utils.app.toast.ToastTintUtils;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;


public class ViewAssistActivity extends BaseActivity {

    private DirectionalLayout bar_Layout;
    private DirectionalLayout bar;
    private Image select_retuen;



    @Override
    protected int baseLayoutId() {
        return ResourceTable.Layout_activity_view_assist;
    }

    @Override
    protected void initView() {
        bar_Layout = (DirectionalLayout) findComponentById(ResourceTable.Id_base_view_barLayout);
        bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_sheet_dialog_bar, null, false);
        text = (Text) bar.findComponentById(ResourceTable.Id_titleText);
        select_retuen = (Image) bar.findComponentById(ResourceTable.Id_sheet_dialog_return);
        bar_Layout.addComponent(bar);
        type = getModuleType();
        vidAvaFrame = (StackLayout) findComponentById(ResourceTable.Id_vid_ava_frame);
    }

    @Override
    protected void initListener() {
        select_retuen.setClickedListener(component -> {
            // LogUtils.error(TAG, "return 点击");
            onBackPressed();
        });
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        bar_Layout.removeAllComponents();
    }

    private static final String TAG = ViewAssistActivity.class.getSimpleName();

    private ViewAssist viewAssist;

    private StackLayout vidAvaFrame;

    private Intent mIntent;

    private int type;

    private Text text;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        mIntent = intent;
        LogUtils.error(TAG, " onStart");
    }

    @Override
    protected void onActive() {
        super.onActive();
        initValue();
        if (bar_Layout.getChildCount() == 0) {
            bar_Layout.addComponent(bar);
        }
    }



    private void initValue() {
        viewAssist = ViewAssist.wrap(vidAvaFrame);
        viewAssist.setContext(this);
       // emptyType();
        if (type == ButtonValue.BTN_VIEW_ASSIST_RECYCLER) {
            text.setText("RecyclerView ( loading )");
        } else if (type == ButtonValue.BTN_VIEW_ASSIST_ERROR){
            text.setText("Error ( failed )");
            errorType();
        } else if (type == ButtonValue.BTN_VIEW_ASSIST_EMPTY) {

            text.setText("Empty ( data )");
            emptyType();
        } else if (type == ButtonValue.BTN_VIEW_ASSIST_CUSTOM) {

            text.setText("Custom Type");
            customType();
        }

        //
     /*   switch (getModuleType()) {
          case ButtonValue.BTN_VIEW_ASSIST_ERROR:
              errorType();
              break;
            case ButtonValue.BTN_VIEW_ASSIST_CUSTOM:
                break;
            case ButtonValue.BTN_VIEW_ASSIST_EMPTY:
                break;
        }*/
    }

    public int getModuleType() {
        return mIntent.getIntParam(DevFinal.TYPE, 0);
    }

    private void errorType() {
       // ViewUtils.setPadding(vidAvaFrame, SizeUtils.dipConvertPx(50f));
        viewAssist.register(ViewAssist.TYPE_ING, new ViewAssist.Adapter() {
            @Override
            public Component onCreateView(ViewAssist assist, LayoutScatter inflater) {
                return new LoadingViewDiv(getContext());
            }

            @Override
            public void onBindView(ViewAssist assist, Component view, int type) {
                boolean isContent = assist.getTag() == null;
                int time;
                if(isContent) time = 3000;
                else time = 4000;
                HandlerUtils.postRunnable(() -> {
                    if (isContent) {
                        assist.showType(100);
                    } else {
                        assist.showType(200);
                    }
                }, time);
            }
        }).register(100, new ViewAssist.Adapter() {
            @Override
            public Component onCreateView(ViewAssist assist, LayoutScatter inflater) {
                return inflater.parse(ResourceTable.Layout_view_assist_error, null, false);
            }

            @Override
            public void onBindView(ViewAssist assist, Component view, int type) {
                view.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        ToastTintUtils.normal("click retry");
                       assist.setTag("").showIng();
                 /*       vidAvaFrame.removeAllComponents();
                        LoadingViewDiv div = new LoadingViewDiv(getContext());
                        vidAvaFrame.addComponent(new LoadingViewDiv(getContext()));*/
                    }
                });
            }
        }).register(200, new ViewAssist.Adapter() {
            @Override
            public Component onCreateView(ViewAssist assist, LayoutScatter inflater) {
                return inflater.parse(ResourceTable.Layout_view_assist_content, null, false);
            }

            @Override
            public void onBindView(ViewAssist assist, Component view, int type) {

            }
        }).showIng();
    }

    public void emptyType() {
        viewAssist.register(ViewAssist.TYPE_ING, new ViewAssist.Adapter() {
            @Override
            public Component onCreateView(ViewAssist assist, LayoutScatter inflater) {
                return new CoolWaitLoadingRenderer(getContext());
            }


            @Override
            public void onBindView(ViewAssist assist, Component view, int type) {

                HandlerUtils.postRunnable(() -> {
                    if (view instanceof CircleBroodLoadingRenderer) {
                        ((CircleBroodLoadingRenderer) view).stop();
                    }
                    assist.showType(Integer.MAX_VALUE);
                }, 3000);
            }
        }).register(Integer.MAX_VALUE, new ViewAssist.Adapter() {

            @Override
            public Component onCreateView(ViewAssist assist, LayoutScatter inflater) {
                return inflater.parse(ResourceTable.Layout_view_assist_error,null, false);
            }

            @Override
            public void onBindView(ViewAssist assist, Component view, int type) {
            }
        }).showIng();
    }


    public void customType() {
        viewAssist.register(ViewAssist.TYPE_ING, new ViewAssist.Adapter() {
            @Override
            public Component onCreateView(ViewAssist assist, LayoutScatter inflater) {
                return new DanceLoadingRenderer(getContext());
            }

            @Override
            public void onBindView(ViewAssist assist, Component view, int type) {
                HandlerUtils.postRunnable(() -> {
                    if (view instanceof CoolWaitLoadingRenderer) {
                        ((CoolWaitLoadingRenderer) view).stop();
                    }
                    assist.showType(Integer.MAX_VALUE);
                }, 3000);
            }
        }).register(Integer.MAX_VALUE, new ViewAssist.Adapter() {
            @Override
            public Component onCreateView(ViewAssist assist, LayoutScatter inflater) {
                return inflater.parse(ResourceTable.Layout_view_assist_custom,null, false);
            }

            @Override
            public void onBindView(ViewAssist assist, Component view, int type) {

            }
        }).showIng();
    }


}
